package com.gismore.demo.config.converter;

import org.aeonbits.owner.Converter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.lang.reflect.Method;

/**
 * Created by lei on 2017/5/26.
 */
public class LongExpressionConverter implements Converter<Long> {
	@Override
	public Long convert(Method method, String rawValue) {
		Long ret = 0L;
		if(StringUtils.isNotBlank(rawValue)){
			if(NumberUtils.isParsable(rawValue)){
				ret = Long.valueOf(rawValue);
			}else{
				ExpressionParser parser = new SpelExpressionParser();
				ret = parser.parseExpression(rawValue).getValue(Long.class);
			}
		}
		return ret;
	}
}
